Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Utils] Replace preserve_attr with patch_attr #1187

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kylesayrs
Copy link
Collaborator

Purpose

  • Provide explicit patching functionality to preserve_attr
  • This function is very similar to unittest.mock.patch, except that using this functionality does not require using the unittest library in source code

Changes

  • Replace preserve_attr with patch_attr
  • Replace usage in src/llmcompressor/pipelines/sequential/helpers.py, which helps with clarity
  • Add unit test mark to utils/helpers pytests

Testing

  • Added tests

Signed-off-by: Kyle Sayers <[email protected]>
Copy link

👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review.

Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed.

@kylesayrs kylesayrs changed the title Replace preserve_attr with patch_attr [Utils] Replace preserve_attr with patch_attr Feb 24, 2025
@kylesayrs kylesayrs self-assigned this Feb 26, 2025
@kylesayrs kylesayrs added the ready When a PR is ready for review label Mar 10, 2025
@@ -1132,9 +1132,39 @@ def calibration_forward_context(model: PreTrainedModel):


@contextlib.contextmanager
def preserve_attr(base: object, attr: str):
value = getattr(base, attr)
def patch_attr(base: object, attr: str, *args, **kwargs):
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why use *args and **kwargs here? It seems like you can just have a patch_value: Any = None input rather than trying to infer based on len(args) or "value" in kwargs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ready When a PR is ready for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants